home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Helpers / Tzu Release 4 / Tool Development / Comment.cp next >
Encoding:
Text File  |  1994-11-05  |  1.5 KB  |  63 lines  |  [TEXT/MMCC]

  1.  
  2. // * Comment Tzu
  3. // * ©1994 Chris K. Thomas.  All Rights Reserved.
  4. // * 
  5. // * 100 11 Oct 94 ckt
  6. // * 101 13 Oct 94 ckt - rewrote to comment more intelligently
  7. // *                         & wrote InsertText & comment
  8.  
  9. #include "TzuTools.h"
  10.  
  11. pascal void main(Handle text);
  12. void InsertText(Handle hText,long insertOffset,long insertLen,const void *insert);
  13.  
  14. // * Main entry point
  15. // text - handle to text to be modified
  16. pascal void main(Handle text)
  17. {
  18.     EnterCodeResource();
  19.     
  20.     const char *commentText    = "//";
  21.     long         sizeOfText    = GetHandleSize(text);
  22.     Ptr            localText    = *text;
  23.     
  24.     // insert “//” before text, since there isn't a return there usually
  25.     InsertText(text,0,2,commentText);
  26.     
  27.     // replace returns with return + “//”
  28.     for(long i=0; i<(sizeOfText-1); i++)
  29.     {
  30.         localText = *text;
  31.         if(localText[i]=='\r')
  32.         {
  33.             InsertText(text,i+1,2,commentText);
  34.             i+=1;
  35.         }
  36.     }
  37.     
  38.     ExitCodeResource();
  39. }
  40.  
  41.  
  42. //InsertText will probably be a callback in the
  43. //robustified TzuTool interface
  44. void InsertText(Handle hText,long insertOffset,long insertLen,const void *insert)
  45. {
  46.     long    hTextLen    = GetHandleSize(hText);
  47.     char    hTextState    = HGetState(hText);
  48.     Ptr        localText    = NULL;
  49.     
  50.     // SetHandleSize might need to move the memory to resize it
  51.     // which it can't do if the handle is locked
  52.     HUnlock(hText);
  53.     SetHandleSize(hText,hTextLen + insertLen);
  54.     if(MemError()!=noErr) return;
  55.     
  56.     HLockHi(hText);
  57.     localText = *hText;
  58.     
  59.     BlockMoveData(&localText[insertOffset],&localText[insertOffset+insertLen],hTextLen - insertOffset);
  60.     BlockMoveData(insert,&localText[insertOffset],insertLen);
  61.     
  62.     HSetState(hText,hTextState);
  63. }